package com.yunhaikeji.core.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.fileupload.IOUtils;


public class WebFileUtils {


    public static void download(String path, HttpServletResponse response){
        download(path, true, response);
    }

    public static void download(String path, boolean cached, HttpServletResponse response){
        File file = new File(path);
        download(file, cached, response);
    }

    public static void download(File file, HttpServletResponse response){
        download(file, true, response);
    }

    public static void download(String fileName, InputStream file, HttpServletResponse response){
        download(fileName, false, null, file, response);
    }

    public static void download(File file, boolean cached, HttpServletResponse response){
        try {
            InputStream input = new FileInputStream(file);
            download(file.getName(), cached, String.valueOf(file.lastModified()), input, response);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void download(String fileName, boolean cached, String lastModified, InputStream input, HttpServletResponse response){
        doDownload(fileName, cached, lastModified, input, response);
    }

    public static void preview(String path, String contentType, HttpServletResponse response){
        File file = new File(path);
        preview(file, true, contentType, response);
    }

    public static void preview(String path, HttpServletResponse response){
        preview(path, true, response);
    }

    public static void preview(String path, boolean cached, HttpServletResponse response){
        File file = new File(path);
        preview(file, cached, response);
    }

    public static void preview(File file, HttpServletResponse response){
        preview(file, true, response);
    }

    public static void preview(File file, boolean cached, HttpServletResponse response){
        try {
            InputStream input = new FileInputStream(file);
            String ext = file.getName().substring(file.getName().lastIndexOf(".") + 1);
            preview(file.getName(), cached, String.valueOf(file.lastModified()), "application/"+ ext, input, response);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void preview(File file, boolean cached, String contentType, HttpServletResponse response){
        try {
            InputStream input = new FileInputStream(file);
            String ext = file.getName().substring(file.getName().lastIndexOf(".") + 1);
            preview(file.getName(), cached, String.valueOf(file.lastModified()), contentType, input, response);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void preview(String fileName, boolean cached, String lastModified, String ext, InputStream input, HttpServletResponse response){
        doPreview(fileName, cached, lastModified, ext, input, response);
    }

    public static void doDownload(String fileName, boolean cached, String lastModified, InputStream input, HttpServletResponse response){
        try {
            response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        manageCache(cached, lastModified, response);
        writeResponse(input, response);
    }

    public static void doPreview(String fileName, boolean cached, String lastModified, String ext, InputStream input, HttpServletResponse response){
        response.setContentType(ext);
        response.setHeader("Content-Disposition", "inline;filename=" + fileName);
        writeResponse(input, response);
    }

    private static void manageCache(boolean cached, String lastModified, HttpServletResponse response){
        if(cached){
            response.addHeader("Cache-Control", "public, max-age=31556926");
            response.addHeader("Last-Modified", lastModified);
        }else{
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate,max-age=0");
            response.setHeader("Pragma", "no-cache, no-store"); // HTTP 1.0.
        }
    }

    private static void writeResponse(InputStream input, HttpServletResponse response){
        try {
            ServletOutputStream out = response.getOutputStream();
            IOUtils.copy(input, response.getOutputStream());
            out.flush();
            input.close();
            out.close();
//	        response.flushBuffer();
        } catch (IOException e) {
//        	e.printStackTrace();
        }
    }
}

